#!/bin/bash
# name: mysqldump.sh
# date: 2019-01-14

USER="ucds"
PASSWORD="ccod"
Date=`date +%Y%m%d`
databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
mkdir -p /Backup/$Date

for db in $databases; do
    if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
        echo "Dumping database: $db"
        mysqldump -u $USER -p$PASSWORD --databases $db > /Backup/$Date/`date +%Y%m%d`.$db.sql
    fi
done

cd  /Backup/$Date && tar zcf $Date-mysql.tar.gz  *sql  &&  scp  $Date-mysql.tar.gz  root@172.100.0.39:/Backup/mysql16
cd /Backup/ ; find ./ -mtime +10 -exec x -rf {} \;
